package com.six.work.controller;

import com.six.work.entity.Ad;
import com.six.work.entity.AdView;
import com.six.work.entity.Announcement;
import com.six.work.entity.AnnouncementView;
import com.six.work.services.AdServices;
import com.six.work.services.AnnouncementServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("ad")
public class AdController {
    @Autowired
    private AdServices adServices;

    @GetMapping("/list")
    public List<AdView> getAllAd(@RequestParam(value="adTitle",defaultValue = "") String adTitle, @RequestParam(value="admin_id",defaultValue = "0") Integer admin_id)
    {
        List<AdView> list = null;
        if(admin_id == 0) {
            list = adServices.findAdByName(adTitle);
        }else{
            list = adServices.findAdByadIDAndannName(adTitle,admin_id);
        }
        return list;
    }

    @GetMapping("/del")
    public Integer deleteByID(@RequestParam("ad_id") Integer ad_id){
        return adServices.DelAdByID(ad_id);
    }

    @PostMapping("/add")
    public Integer InsertAd(@RequestBody Ad ad){

        return adServices.insertAd(ad);
    }

    @GetMapping("/update")
    public Ad getUpdatePage(@RequestParam("ad_id") Integer ad_id){
        return adServices.SelectAdByID(ad_id);
    }

    @PostMapping("/update")
    public Integer UpdateAd(@RequestBody Ad ad) {
        return adServices.updateAd(ad);
    }

    @GetMapping("/ad")
    public List<Ad> findAd(){
        return adServices.findAd();
    }

    @GetMapping("/ad2")
    public List<Ad> findAd2(){
        return adServices.findAd2();
    }
}
